﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class userControls_RollupFilters : System.Web.UI.UserControl
{
    private string lnkbtn = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dropMonth.DataBind();
            if (!IsPostBack)
            {
                getCookie(chkStacked.ID, chkStacked);
                getCookie(chkShowTables.ID, chkShowTables);
                getCookie(chkShowSummary.ID, chkShowSummary);
                getCookie(chkSyncDaily.ID, chkSyncDaily);
                getCookie(chkShowZeroDays.ID, chkShowZeroDays);
                getCookie(chlLocations.ID, chlLocations);
                getCookie(chlDwellings.ID, chlDwellings);
                getCookie(chlProducts.ID, chlProducts);
                getCookie(optDisplay.ID, optDisplay);
                getCookie(dropMonth.ID, dropMonth);
                getCookie(optDailyChart.ID, optDailyChart);
  
            }

        }

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        //
        RefreshMetadata(sender, e);
    }


    public void RefreshMetadata(object sender, EventArgs e)
    {
        RollupMetadata meta = new RollupMetadata();
        DateTime dtExp = DateTime.Now.AddDays(30);
        meta.ChkStacked = chkStacked; setCookie(chkStacked.ID, chkStacked.Checked.ToString(), dtExp);
        meta.chkShowTables = chkShowTables; setCookie(chkShowTables.ID, chkShowTables.Checked.ToString(), dtExp);
        meta.chkShowSummary = chkShowSummary; setCookie(chkShowSummary.ID, chkShowSummary.Checked.ToString(), dtExp);
        meta.ChlNatl = chlLocations; setCookie(chlLocations.ID, string.Format("{0},{1}", chlLocations.Items[0].Selected, chlLocations.Items[1].Selected), dtExp);
        meta.ChlUnits = chlDwellings; setCookie(chlDwellings.ID, string.Format("{0},{1}", chlDwellings.Items[0].Selected, chlDwellings.Items[1].Selected), dtExp);
        meta.ChlProducts = chlProducts; setCookie(chlProducts.ID, string.Format("{0},{1},{2}", chlProducts.Items[0].Selected, chlProducts.Items[1].Selected, chlProducts.Items[2].Selected), dtExp);
        meta.OplDisplay = optDisplay; setCookie(optDisplay.ID, optDisplay.SelectedIndex.ToString(), dtExp);
        meta.ChkSyncDaily = chkSyncDaily; setCookie(chkSyncDaily.ID, chkSyncDaily.Checked.ToString(), dtExp);
        meta.ChkShowZeroDays = chkShowZeroDays; setCookie(chkShowZeroDays.ID, chkShowZeroDays.Checked.ToString(), dtExp);
        meta.MonthsPrior = Convert.ToInt16(dropMonth.SelectedValue); setCookie(dropMonth.ID, dropMonth.SelectedValue.ToString(), dtExp);
        meta.DailyChartStyle = optDailyChart.SelectedValue; setCookie(optDailyChart.ID, optDailyChart.SelectedIndex.ToString(), dtExp);

        meta.Refresh();
        Session["RollupMetadata"] = meta;


    }


    protected void chkAllDimensions_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < chlProducts.Items.Count; i++) chlProducts.Items[i].Selected = chkAllDimensions.Checked;
        for (int i = 0; i < chlLocations.Items.Count; i++) chlLocations.Items[i].Selected = chkAllDimensions.Checked;
        for (int i = 0; i < chlDwellings.Items.Count; i++) chlDwellings.Items[i].Selected = chkAllDimensions.Checked;
        RefreshMetadata(sender, e);
    }

    protected void getCookie(string psCookieName, CheckBox chk)
    {
        string sCookie = psCookieName;
        chk.Checked = Request.Cookies[sCookie] == null ? false :
       Convert.ToBoolean(  Request.Cookies[sCookie].Value);
    }

    protected void getCookie(string psCookieName, RadioButtonList optlst)
    {
        string sCookie = psCookieName;
        optlst.SelectedIndex = Request.Cookies[sCookie] == null ? 0 :
       Convert.ToInt16(Request.Cookies[sCookie].Value);
    }

    protected void getCookie(string psCookieName, CheckBoxList chklst)
    {
        string sCookie = psCookieName;

        string[] saSelected = Request.Cookies[sCookie] == null ? new string[0] :
       Request.Cookies[sCookie].Value.ToString().Split(',');
        for (int i = 0; i < saSelected.Length; i++) 
            chklst.Items[i].Selected = Convert.ToBoolean( saSelected[i]);

    }

    protected void getCookie(string psCookieName, DropDownList ddl)
    {
        string sCookie = psCookieName;
        ddl.SelectedIndex = Request.Cookies[sCookie] == null ? 0 :
       Convert.ToInt16(Request.Cookies[sCookie].Value);
    }

    protected void setCookie(string psCookie, string psValue, DateTime pdtExp)
    {
        if (Request.Cookies[psCookie] == null)
            Response.Cookies.Add(new HttpCookie(psCookie, psValue));
        else
            Response.Cookies[psCookie].Value = psValue;
        Request.Cookies[psCookie].Value = psValue;
        Response.Cookies[psCookie].Expires = pdtExp;
    }


}